package ustc.xiaoyang.service.impl;

import org.springframework.stereotype.Service;
import ustc.xiaoyang.exception.DeptHasEmpException;
import ustc.xiaoyang.mapper.DeptMapper;
import ustc.xiaoyang.mapper.EmpMapper;
import ustc.xiaoyang.mapper.StudentMapper;
import ustc.xiaoyang.pojo.Dept;
import ustc.xiaoyang.service.DeptService;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {

    private final DeptMapper deptMapper;
    private final EmpMapper empMapper;
    // 构造方法注入
    public DeptServiceImpl(DeptMapper deptMapper,EmpMapper empMapper) {
        this.deptMapper = deptMapper;
        this.empMapper = empMapper;
    }
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void deleteById(Integer id) {
        if(empMapper.countByDeptId(id) > 0){
            throw new DeptHasEmpException("该部门下有员工，不能删除");
        }
        deptMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

}
